<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('疾病管理列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="col-sm-12 search-collapse ifcomebody1">
        <form id="formId" class="layui-form">
            <div class="select-list">
                <ul>
                    <li class="layui-form-item">
                        <label>科目</label>
                        <select id="selectpid" name="pid" lay-filter="selectpid">

                        </select>
                    </li>
                    <li>
                        <label>疾病</label>
                        <select name="id" id="selectpid1">

                        </select>
                    </li>
                    <li>
                        <a class="btn btn-chongzhi btn-rounded mr16 btn-sm" onClick="$.form.reset()">重置</a>
                        <a class="btn btn-chaxun btn-rounded btn-sm" onClick="$.treeTable.search()"> <i
                                class="fa fa-spinner fa-spin divhidden"></i> 查询</a>
                    </li>
                </ul>
            </div>
        </form>
    </div>

    <div class="btn-group-sm" style="display: flex;" id="toolbar" role="group">
        <a class="btn btn-dataadd" onClick="$.operate.add()" shiro:hasPermission="old:disease:add">
            <i class="fa fa-plus"></i> 新增
        </a>
        <a class="btn btn-newadd" id="expandAllBtn">
            <i class="fa fa-exchange"></i> 展开/折叠
        </a>
    </div>
    <div class="col-sm-12 select-table table-striped ifcomebody">
        <table id="bootstrap-tree-table"></table>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script>

</script>
<script th:inline="javascript">
    let pdata
    var addFlag = [[${@permission.hasPermi('old:disease:add')}]];
    var editFlag = [[${@permission.hasPermi('old:disease:edit')}]];
    var removeFlag = [[${@permission.hasPermi('old:disease:remove')}]];
    var prefix = ctx + "old/disease";
    layui.use(['form'], () => {
        var form = layui.form
        $(function () {
            var options = {
                code: "id",
                parentCode: "pid",
                expandColumn: "1",
                expandAll: false,
                sortName: "createTime",
                sortOrder: "desc",
                expandFirst: false,
                uniqueId: "id",
                url: prefix + "/list",
                createUrl: prefix + "/add/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove/{id}",
                modalName: "疾病管理",
                onLoadSuccess: (data) => {
                    pdata = data
                    let option = []
                    option = data.map((item, index) => {
                        let op = '';
                        if (item.pid == 0) {
                            op = "<option value='" + item.id + "'>" + item.name + "</option>"
                        }
                        return op
                    })
                    option.unshift("<option value=''>请选择科目</option>")
                    var ophtml = option.join('')
                    $('#selectpid').append(ophtml)
                    select2()
                    form.render()
                },
                columns: [{
                    field: 'selectItem',
                    radio: true
                },
                    {
                        field: 'name',
                        title: '疾病名称',
                        align: 'left',
                        width: "400"
                    },{
                        field: 'sortid',
                        title: '排序',
                        align: 'left'
                    },

                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'left'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        align: 'left',
                        formatter: function (value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-caozuo btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')">编辑</a> ');
                            actions.push('<a class="btn btn-caozuo  btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="$.operate.add(\'' + row.id + '\')">新增</a> ');
                            actions.push('<a class="btn btn-caozuo btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')">删除</a>');
                            return actions.join('');
                        }
                    }]
            };
            $.treeTable.init(options);
        });

        form.on('select(selectpid)', function (data) {
            let id1 = data.value
            let option = []
            if (id1) {
                option = pdata.map((item, index) => {
                    if (item.pid == id1) {
                        return "<option value='" + item.id + "'>" + item.name + "</option>"
                    }
                })
            }
            select2(option)
        });

        function select2(option = []) {
            option.unshift("<option value=''>请选择疾病</option>")
            var ophtml = option.join('')
            $('#selectpid1').html(ophtml)
            form.render()
        }
    })

    /*推荐状态显示 */
    function statusTools(row) {
        if (row.isRecommend == 0) {
            return '<img class="list-switch" src="/img/list-switch2.svg" onclick="enable(\'' + row.id + '\')"></img> ';
        } else {
            return '<img class="list-switch" src="/img/list-switch1.svg" onclick="disable(\'' + row.id + '\')"></img> ';
        }
    }

    /* 推荐-停用 */
    function disable(id) {
        $.modal.popUpConfirm("确认要取消推荐吗？", function () {
            $.operate.post(prefix + "/edit", {"id": id, "isRecommend": 0});
        })
    }

    /* 推荐启用 */
    function enable(id) {
        $.modal.popUpConfirm("确认要启用推荐吗？", function () {
            $.operate.post(prefix + "/edit", {"id": id, "isRecommend": 1});
        })
    }

</script>

</body>
</html>